<template>
  <el-form
    :model="productForm"
    label-width="120px"
    ref="productFormRef"
    class="product-form"
  >
    <!-- 产品名 -->
    <el-form-item
      label="产品名"
      prop="pro_name"
    >
      <el-input v-model="productForm.pro_name" />
    </el-form-item>

    <!-- 产品详情 -->
    <el-form-item
      label="产品详情"
      prop="pro_detail"
    >
      <el-input
        type="textarea"
        v-model="productForm.pro_detail"
        :rows="4"
      />
    </el-form-item>

    <!-- 上线时间 -->
    <el-form-item
      label="上线时间"
      prop="pro_time"
    >
      <el-date-picker
        v-model="productForm.pro_time"
        type="date"
        placeholder="选择日期"
        value-format="YYYY-MM-DD"
        style="width: 100%"
      />
    </el-form-item>

    <!-- 价格 -->
    <el-form-item
      label="价格"
      prop="pro_price"
    >
      <div class="price-container">
        <el-input
          v-model="priceMin"
          placeholder="最低价"
          class="price-input"
        />
        <span class="price-separator">-</span>
        <el-input
          v-model="priceMax"
          placeholder="最高价"
          class="price-input"
        />
        <el-select
          v-model="pro_price_unit"
          placeholder="单位"
          class="price-unit"
        >
          <el-option
            label="百"
            value="百"
          />
          <el-option
            label="千"
            value="千"
          />
          <el-option
            label="万"
            value="万"
          />
        </el-select>
      </div>
    </el-form-item>

    <!-- 图片上传 -->
    <el-form-item
      label="产品图片"
      prop="pro_filename"
    >
      <el-upload
        ref="uploadRef"
        action="#"
        list-type="picture-card"
        :auto-upload="false"
        :on-change="handleFileChange"
        :limit="1"
        :file-list="fileList"
        class="upload-container"
      >
        <el-button type="primary">点击上传</el-button>
      </el-upload>
    </el-form-item>

    <!-- 提交按钮 -->
    <el-form-item class="button-group">
      <el-button
        type="primary"
        @click="submitForm"
        class="submit-button"
      >提交</el-button>
      <el-button @click="resetForm" class="reset-button">重置</el-button>
    </el-form-item>
  </el-form>
  <el-overlay
    v-if="showProgress"
    :show="showProgress"
    @click="() => {}"
  >
    <div class="progress-container">
      <el-progress
        :percentage="progressPercent"
        :status="progressStatus"
        type="circle"
      />
    </div>
  </el-overlay>
</template>

<script>
import Axios from "axios";
import { ElMessage,ElOverlay } from "element-plus";
export default {
  components: { ElOverlay },
  data() {
    return {
      fileList: [],
      priceMin: "",
      priceMax: "",
      pro_price_unit: "",
      productForm: {
        pro_name: "",
        pro_detail: "",
        pro_time: new Date(),
        pro_price: "",
        pro_filename: "",
      },

      showProgress: false,
      progressPercent: 0,
      progressStatus: "",
    };
  },
  methods: {
    submitForm() {
      console.log(this.productForm.pro_time);
      this.productForm.pro_price =
        this.priceMin +
        this.pro_price_unit +
        "-" +
        this.priceMax +
        this.pro_price_unit;
      const formData = new FormData();
      formData.append("pro_name", this.productForm.pro_name);
      formData.append("pro_detail", this.productForm.pro_detail);
      formData.append("pro_time", this.productForm.pro_time);
      formData.append("pro_price", this.productForm.pro_price);
      //图片
      formData.append("file", this.productForm.pro_filename);
      //初始化进度条
      this.showProgress = true;
      this.progressStatus = "";
      this.progressPercent = 0;
      console.log(this.showProgress);
      const interval = setInterval(() => {
        if (this.progressPercent < 90) {
          this.progressPercent += 1;
        }
      }, 20);
      //发起请求
      this.addProduct(formData, interval);
      console.log("提交操作", this.productForm);
    },
    resetForm() {
      console.log("重置操作");
    },
    handleFileChange(file, fileList) {
      this.fileList = fileList;
      if (file.raw) {
        this.productForm.pro_filename = file.raw;
      }
    },
    async addProduct(formData, interval) {
      try {
        const resp = await Axios.post(
          "http://47.108.80.35:6060/company_req_product/add_product",
          formData,
          {
            headers: {
              "Content-Type": "multipart/form-data",
            },
          }
        );
        console.log(resp);
        clearInterval(interval);
        this.progressPercent = 100;
        this.progressStatus = "success"; //状态
        setTimeout(() => {
          this.showProgress = false;
        }, 2000);
      } catch (error) {
        console.error(error);
        ElMessage.error("请检查网络，或产品信息");
        clearInterval(interval);
        this.progressPercent = 0;
        this.progressStatus = "exception";
        setTimeout(() => {
          this.showProgress = false;
        }, 2000);
      }
    },
  },
};
</script>
<style scoped>
.product-form {
  width: 90%;
  max-width: 600px;
  margin: 20px auto;
  padding: 15px;
  background-color: #f9f9f9;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  box-sizing: border-box;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-form-item__label {
  font-weight: bold;
  color: #333;
  font-size: 0.9rem;
}

.el-input,
.el-textarea,
.el-input-number {
  border-radius: 6px;
}

.price-container {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.price-input {
  flex: 1;
  min-width: 100px;
}

.price-separator {
  margin: 0 5px;
}

.price-unit {
  width: 100px;
}

.upload-container {
  width: 100%;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 30px;
}

.submit-button,
.reset-button {
  border-radius: 6px;
  padding: 10px 20px;
  font-size: 1rem;
  min-width: 100px;
  margin-right: 10%;
}

.el-form-item .el-button + .el-button {
  margin-left: 0;
}

.el-upload-list--picture-card .el-upload {
  border-radius: 8px;
}

.progress-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

::v-deep .el-progress__text {
  color: white;
}

/* 平板设备 */
@media (min-width: 768px) {
  .product-form {
    width: 80%;
    margin: 30px auto;
    padding: 20px;
  }
  
  .el-form-item {
    margin-bottom: 25px;
  }
  
  .el-form-item__label {
    font-size: 1rem;
  }
  
  .price-container {
    flex-wrap: nowrap;
  }
  
  .price-input {
    min-width: 120px;
  }
  
  .price-unit {
    width: 100px;
  }
  
  .submit-button,
  .reset-button {
    padding: 12px 25px;
    font-size: 1.1rem;
    min-width: 120px;
  }
}

/* 桌面设备 */
@media (min-width: 1024px) {
  .product-form {
    width: 70%;
    max-width: 600px;
    margin: 40px auto;
    padding: 30px;
  }
  
  .el-form-item {
    margin-bottom: 30px;
  }
  
  .price-input {
    min-width: 150px;
  }
  
  .price-unit {
    width: 100px;
  }
}

/* 小屏幕优化 */
@media (max-width: 480px) {
  .product-form {
    width: 95%;
    padding: 12px;
    margin: 15px auto;
  }
  
  .el-form-item__label {
    font-size: 0.85rem;
  }
  
  .price-container {
    gap: 5px;
  }
  
  .price-input {
    min-width: 80px;
    font-size: 0.9rem;
  }
  
  .price-separator {
    margin: 0 3px;
  }
  
  .price-unit {
    width: 80px;
    font-size: 0.9rem;
  }
  
  .submit-button,
  .reset-button {
    padding: 8px 15px;
    font-size: 0.9rem;
    min-width: 80px;
  }
}
</style>